package com.hex.ds.hdrs.engine.interceptor;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Package: com.hex.dataService.hdrs.scheduleService.interceptor
 * @ClassName ChannelFilter
 * @Description:过滤器Filter，用来把request传递下去
 * @Author: <gc.zuo>
 * @Date 2021/6/8 9:09
 * @Version v1.0
 **/
@WebFilter(urlPatterns = { "/api/cronTaskDef/add", "/api/jobDef/add"}, filterName = "channelFilter")
public class ChannelFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            ServletRequest requestWrapper = null;
            if (servletRequest instanceof HttpServletRequest) {
                requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
            }
            if (requestWrapper == null) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                filterChain.doFilter(requestWrapper, servletResponse);
            }
        } catch (Exception e) {
            System.out.println(e.toString());

        }
    }

    @Override
    public void destroy() {
    }
}